package org.phytodata.actionengine;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.pentaho.platform.api.engine.IPentahoSession;
import org.pentaho.platform.engine.core.system.PentahoSessionHolder;
import org.pentaho.platform.engine.core.system.PentahoSystem;
import org.pentaho.platform.util.beans.ActionHarness;

public class GroovyEngineServlet extends HttpServlet {
  
  private static final long serialVersionUID = 1584486522001535135L;
  private static final Log log = LogFactory.getLog(GroovyEngineServlet.class);

  @SuppressWarnings("nls")
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//    String scriptRootDirName = "/home/aaron/tmp/dev-biplatform/biserver-ce/pentaho-solutions/system/groovyengine-plugin/resources/";
//    String scriptFileName = "hello.groovy";
//    ISolutionRepository repo = PentahoSystem.get(ISolutionRepository.class, PentahoSessionHolder.getSession());
    String path = req.getParameter("path");
    String solution = req.getParameter("solution");
    String fileName = req.getParameter("file");
//    ISolutionFile solFile = repo.getSolutionFile(solution+"/"+path+"/"+fileName, ISolutionRepository.ACTION_EXECUTE);
    
    String fullSolutionPath = PentahoSystem.getApplicationContext().getSolutionPath(solution+"/"+path);
    File solutionFile = new File(fullSolutionPath, fileName);
    if (!solutionFile.exists()) {
      log.error("File not found :" + solutionFile.getAbsolutePath());
      resp.sendError(404, "Solution file not found :" + solutionFile.getAbsolutePath());
    }
    
    String scriptRootDirName = fullSolutionPath; // "/pentaho-solutions/bi-developers/GroovyEngine"
    String scriptFileName = fileName;   //<--- HERE HERE HERE!!!  trying to figure out the path to the groovy file.  
    
    GroovyEngineAction action = new GroovyEngineAction();
    ActionHarness harness = new ActionHarness(action);
    
    @SuppressWarnings("unchecked")
    Map<String, Object> inputMap = new HashMap<String, Object>(req.getParameterMap());
	//FIXME:  this is Linux only file pathing. We should really be creating a Reader
	//        or an InputStream in this servlet, pass it to the IAction and let the 
	//        IAction do the actual reading
    inputMap.put("scriptRootDirName", scriptRootDirName);
    inputMap.put("scriptFileName", scriptFileName);
    inputMap.put("scriptOutputStream", resp.getOutputStream());
    
    //because we are in a request, pass the session on to the script as well
    inputMap.put("session", PentahoSessionHolder.getSession());
    
    try {
      harness.setValues(inputMap);
      action.execute();
    } catch (Exception e) {
      log.error("GroovyEngineServlet failed to execute script", e);
      resp.sendError(500, "GroovyEngineServlet failed to execute script: "+e.getMessage()+" See log for more details");
    }
  }
  

}
